package com.projectzqjz.parttimework.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.projectzqjz.parttimework.App;
import com.projectzqjz.parttimework.R;
import com.projectzqjz.parttimework.base.BaseActivity;
import com.projectzqjz.parttimework.utils.NewsToastUtils;
import com.projectzqjz.parttimework.utils.ScreenUtil;
import com.projectzqjz.parttimework.utils.TitleBuilder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

import static com.projectzqjz.parttimework.base.Constant.COMPANY;
import static com.projectzqjz.parttimework.base.Constant.DELETE;
import static com.projectzqjz.parttimework.base.Constant.JOB;
import static com.projectzqjz.parttimework.base.Constant.SAVE;


public class PartWorkActivity extends BaseActivity {
    @BindView(R.id.tv_title_right)
    TextView tv_title_right;


    @BindView(R.id.tv_company)
    TextView tv_company;
    @BindView(R.id.tv_job)
    TextView tv_job;
    @BindView(R.id.tv_time1)
    TextView tv_time1;
    @BindView(R.id.tv_time2)
    TextView tv_time2;

    @BindView(R.id.ed_content)
    EditText ed_content;

    @BindView(R.id.iv_delete)
    ImageView iv_delete;

    private TimePickerView pvCustomLunar;
    private int type = -1;
    private String position = "-1";//选中的位置

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    //检测时间大小
    private boolean checkTime(String time1, String time2) {
        String[] start = time1.split("\\.");
        String[] end = time2.split("\\.");
        if (Integer.parseInt(start[0]) < Integer.parseInt(end[0])) {
            return true;
        } else if (Integer.parseInt(start[0]) == Integer.parseInt(end[0])) {
            if (Integer.parseInt(start[1]) <= Integer.parseInt(end[1])) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_part_work;
    }

    @OnClick({R.id.tv_title_right, R.id.iv_delete, R.id.rel_add_time1, R.id.rel_add_time2
            , R.id.rel_company_name, R.id.rel_job_name
    })
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_title_right: //保存工作经历

                String companyName = tv_company.getText().toString();
                String time1 = tv_time1.getText().toString();
                String time2 = tv_time2.getText().toString();
                String job = tv_job.getText().toString();
                String content = ed_content.getText().toString();
                if ("".equals(companyName)) {
                    NewsToastUtils.showToast(App.getContext(), "公司名字不能为空！");
                } else if ("".equals(job)) {
                    NewsToastUtils.showToast(App.getContext(), "任职岗位不能为空！");
                } else if ("".equals(time1)) {
                    NewsToastUtils.showToast(App.getContext(), "入职时间不能为空！");
                } else if ("".equals(time2)) {
                    NewsToastUtils.showToast(App.getContext(), "离职时间不能为空！");
                } else if ("".equals(content)) {
                    NewsToastUtils.showToast(App.getContext(), "工作内容不能为空！");
                } else {
                    if (checkTime(time1,time2)){
                        Intent it = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putString("company_name", companyName);
                        bundle.putString("time", time1 + "-" + time2);
                        bundle.putString("job", job);
                        bundle.putString("content", content);
                        bundle.putString("position", position);
                        bundle.putString("SAVE_DELETE", SAVE + "");
                        it.putExtra("workModel", bundle);
                        setResult(RESULT_OK, it);
                        finish();
                    }else {
                        NewsToastUtils.showToast(App.getContext(),"入职时间不能小于或等于离职时间！");
                    }

                }

                break;
            case R.id.iv_delete://删除简历
                Intent it_1 = new Intent();
                Bundle bundle_1 = new Bundle();
                bundle_1.putString("company_name", "");
                bundle_1.putString("time", "");
                bundle_1.putString("job", "");
                bundle_1.putString("content", "");
                bundle_1.putString("position", position);
                bundle_1.putString("SAVE_DELETE", DELETE + "");
                it_1.putExtra("workModel", bundle_1);
                setResult(RESULT_OK, it_1);
                finish();
                break;
            case R.id.rel_add_time1://添加入职时间
                type = 0;
                pvCustomLunar.show();
                break;
            case R.id.rel_add_time2://添加离职时间
                type = 1;
                pvCustomLunar.show();
                break;
            case R.id.rel_company_name://公司名称
                startActivityForResult(new Intent(this, PartEditorActivity.class)
                        .putExtra("title", "公司名称"), COMPANY);
                break;
            case R.id.rel_job_name://工作岗位
                startActivityForResult(new Intent(this, PartEditorActivity.class)
                        .putExtra("title", "任职岗位"), JOB);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            assert data != null;
            String name = data.getStringExtra("name");
            switch (requestCode) {
                case COMPANY://公司名称
                    tv_company.setText(name);
                    break;
                case JOB://任职岗位
                    tv_job.setText(name);
                    break;
            }
        }
    }

    //弹出时间滑动选择
    @SuppressLint("SimpleDateFormat")
    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String str = format.format(date);
        return str.split("-")[0] + "." + str.split("-")[1];
    }

    @SuppressLint("WrongConstant")
    private void initLunarPicker() {
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(1950, 0, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2019, 11, 1);
        //时间选择器 ，自定义布局
        pvCustomLunar = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                switch (type) {
                    case 0:
                        tv_time1.setText(getTime(date));
                        break;
                    case 1:
                        tv_time2.setText(getTime(date));
                        break;
                }
            }
        })
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {

                    @Override
                    public void customLayout(final View v) {
                        TextView tvSubmit = (TextView) v.findViewById(R.id.tv_submit);//确定
                        TextView tvCanncel = (TextView) v.findViewById(R.id.tv_cancel);//取消
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                pvCustomLunar.returnData();
                                pvCustomLunar.dismiss();
                            }
                        });
                        tvCanncel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                pvCustomLunar.returnData();
                                pvCustomLunar.dismiss();
                            }
                        });

                    }
                })
                .setType(new boolean[]{true, true, false, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(getResources().getColor(R.color.colorEEE))
                .build();
    }

    @Override
    public void initView() {
        initLunarPicker();
        tv_title_right.setText("保存");
        new TitleBuilder(this).setTitleText("工作经历")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });

    }

    @Override
    public void initData() {
        Intent it = getIntent();
        String state = it.getStringExtra("state");
        assert state != null;
        switch (state) {
            case "add":
                iv_delete.setVisibility(View.GONE);
                break;
            case "editor":
                iv_delete.setVisibility(View.VISIBLE);
                Bundle bundle = it.getExtras();
                if (bundle != null) {
                    String company_name = bundle.getString("company_name");
                    String time = bundle.getString("time");
                    String job = bundle.getString("job");
                    String content = bundle.getString("content");
                    position = bundle.getString("position");
                    assert time != null;
                    tv_company.setText(company_name);
                    tv_job.setText(job);
                    tv_time1.setText(time.split("-")[0]);
                    tv_time2.setText(time.split("-")[1]);
                    ed_content.setText(content);
                    ed_content.setSelection(ed_content.getText().length());
                }
                break;
        }
    }
}
